package com.csd.dao;
// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.csd.pojo.Teacher;

/**
 	* A data access object (DAO) providing persistence and search support for Teacher entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Teacher
  * @author MyEclipse Persistence Tools 
 */

public class TeacherDAO extends HibernateDaoSupport  {
    private static final Log log = LogFactory.getLog(TeacherDAO.class);
	//property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String NAME = "name";
	public static final String EMAIL = "email";
	public static final String PHONE = "phone";
	public static final String WEBSITE = "website";
	public static final String INTRODUCTION = "introduction";
	public static final String STATUS = "status";



	protected void initDao() {
		//do nothing
	}
    
    public void save(Teacher transientInstance) {
        log.debug("saving Teacher instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Teacher persistentInstance) {
        log.debug("deleting Teacher instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Teacher findById(Integer id) {
        log.debug("getting Teacher instance with id: " + id);
        try {
            Teacher instance = (Teacher) getHibernateTemplate()
                    .get(Teacher.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
	/**
	 * 列出该项目的评审项
	 * @param projectID 项目id
	 * @author sanmu
	 * @return 教师,项目，评审对象列表
	 */
    public List listEvaluateTeacher(Integer projectID)
    {
        log.debug("finding evaluate Teacher instance by projectID");
        try {
        	String hqlString = "from Teacher as t, Project as p, EvaluateItem as e "+
        	" where e.project.id = ? and e.teacher.id = t.id";
            List results = getHibernateTemplate().find(hqlString, projectID);
            log.debug("find by projectID successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by project failed", re);
            throw re;
        }
    }
    
	/**
	 * 列出可供选择的老师列表
	 * @author sanmu
	 * @return 教师,项目，评审对象列表
	 */
    public List listSelectTeacher()
    {
        log.debug("finding free evaluate Teacher instance");
        try {
        	String hqlString = "from Teacher as t, Project as p, EvaluateItem as e "+
        	" where p.teacher.id != t.id and e.teacher.id != t.id";
            List results = getHibernateTemplate().find(hqlString);
            log.debug("find free evaluate Teacher successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find free evaluate Teacher failed", re);
            throw re;
        }
    }
    
    public List findByExample(Teacher instance) {
        log.debug("finding Teacher instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Teacher instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Teacher as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByUsername(Object username
	) {
		return findByProperty(USERNAME, username
		);
	}
	
	public List findByPassword(Object password
	) {
		return findByProperty(PASSWORD, password
		);
	}
	
	public List findByName(Object name
	) {
		return findByProperty(NAME, name
		);
	}
	
	public List findByEmail(Object email
	) {
		return findByProperty(EMAIL, email
		);
	}
	
	public List findByPhone(Object phone
	) {
		return findByProperty(PHONE, phone
		);
	}
	
	public List findByWebsite(Object website
	) {
		return findByProperty(WEBSITE, website
		);
	}
	
	public List findByIntroduction(Object introduction
	) {
		return findByProperty(INTRODUCTION, introduction
		);
	}
	
	public List findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	

	public List findAll() {
		log.debug("finding all Teacher instances");
		try {
			String queryString = "from Teacher";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Teacher merge(Teacher detachedInstance) {
        log.debug("merging Teacher instance");
        try {
            Teacher result = (Teacher) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Teacher instance) {
        log.debug("attaching dirty Teacher instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Teacher instance) {
        log.debug("attaching clean Teacher instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
   public void updateTeacher(Teacher teacher){
	   try {
		     this.getHibernateTemplate().saveOrUpdate(teacher);
	} catch (Exception e) {
			System.out.println("teacherDAO.");
	}
	   
   }
   
 
   
	public static TeacherDAO getFromApplicationContext(ApplicationContext ctx) 
	{
    	return (TeacherDAO) ctx.getBean("TeacherDAO");
	}
	
	/**
	 * 列出所有老师除了指导老师之外
	 * 
	 * @return 老师实例列表
	 */
	@SuppressWarnings("unchecked")
	public List<Teacher> findTeachersExceptInstructor(Integer projectId) {
		log.debug("finding Teacheres instances except instructor ");

		try {
			String queryString = "select new Teacher(t.name, t.id) from Teacher t, Project p where p.id = ? AND t.id != p.teacher.id";
			List<Teacher> results = getHibernateTemplate().find(queryString, projectId);
			log.debug("自动分配评审老师出错, result size: " + results.size());
			return results;
		} catch (RuntimeException re) 
		{
			log.error("find all failed", re);
			throw re;
		}
	}
}